package net.pet.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class AbstractHttpFilter  implements Filter{

	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
	    	   throws IOException, ServletException;

    protected boolean isFilterPattern(HttpServletRequest request) {
        String visitUri = getVisitUri(request);
        if (!visitUri.endsWith(".htm") && !visitUri.endsWith(".json")) {
            return true;
        } else if (visitUri.startsWith("/login") || visitUri.equals("/logout.htm") || visitUri.startsWith("/index")) {
            return true;
        } else {
            return false;
        }
    }
    protected String getVisitUri(HttpServletRequest request) {
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String visitUri = requestUri.substring(contextPath.length());
        return visitUri;
    }
}
